package com.example.animation;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private Button mAlpha;
    private Button mScale;
    private Button mRotate;
    private Button mTranslate;
    private ImageView mIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAlpha = (Button) findViewById(R.id.Alpha);
        mScale = (Button) findViewById(R.id.Scale);
        mRotate = (Button) findViewById(R.id.Rotate);
        mTranslate = (Button) findViewById(R.id.Translate);

        mIv = (ImageView) findViewById(R.id.iv);

        mAlpha.setOnClickListener(new AlphaListener());         //淡入淡出动画
        mScale.setOnClickListener(new ScaleListener());         //缩放动画
        mRotate.setOnClickListener(new RotateListener());       //旋转动画
        mTranslate.setOnClickListener(new TranslateListener()); //移动动画
    }

    //淡入淡出动画
    class AlphaListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            //创建AnimationSet对象,参数为boolean型
            // true表示使用Animation的interpolator,false则是使用自己的
            AnimationSet animationSet = new AnimationSet(true);
            //创建Animation对象,参数从完全透明度到完全的不透明
            AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
            //设置动画执行的时间
            alphaAnimation.setDuration(6000);
            //将animation对象添加到animationSet中
            animationSet.addAnimation(alphaAnimation);
            //动画结束后停留在动画结束时的状态
            alphaAnimation.setFillAfter(true);
            //使用ImageView的startAnimation方法执行动画
            mIv.startAnimation(alphaAnimation);
        }
    }

    //缩放动画
    class ScaleListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            AnimationSet animationSet = new AnimationSet(true);
            /**
             * 参数1,X轴的初始值
             * 参数2,X轴收缩后的值
             * 参数3,Y轴的初始值
             * 参数4,Y轴收缩后的值
             * 参数5,确定X轴坐标的类型
             * 参数6,X轴的值,0.5f表明是以自身这个控件的一半长度为X轴
             * 参数7,确定Y轴坐标的类型
             * 参数8,Y轴的值,0.5f表明是以自身这个控件的一半长度为X轴
             * */
            ScaleAnimation scaleAnimation = new ScaleAnimation(
                    1,0.5f,1,0.5f,
                    Animation.RELATIVE_TO_SELF,0.5f,
                    Animation.RELATIVE_TO_SELF,0.5f
            );
            scaleAnimation.setDuration(1000);
            animationSet.addAnimation(scaleAnimation);
            scaleAnimation.setFillAfter(true);
            mIv.startAnimation(animationSet);
        }
    }

    //旋转动画
    class RotateListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            AnimationSet animationSet = new AnimationSet(true);
            /**
             * 参数1,从哪个角度开始
             * 参数2,转到什么角度
             * 后四个参数用于设置围绕着旋转的圆的圆心在哪里
             * 参数3,确定X轴坐标类型,ABSOLUT绝对坐标,RELATIVE_TO_SELF相对于自身坐标
             *      RELATIVE_TO_PARENT相对于父控件的坐标
             * 参数4,X轴的值,0.5f表明是以自身这个控件的一半长度为X轴
             * 参数5,确定Y轴坐标类型
             * 参数6,Y轴的值,0.5f表明是以自身这个控件的一半长度为Y轴
             * */
            RotateAnimation rotateAnimation = new RotateAnimation(
                    0,180,
                    Animation.RELATIVE_TO_SELF,0.5f,
                    Animation.RELATIVE_TO_SELF,0.f
            );
            rotateAnimation.setDuration(1000);
            animationSet.addAnimation(rotateAnimation);
            rotateAnimation.setFillAfter(true);
            mIv.startAnimation(animationSet);

        }
    }

    //移动动画
    class TranslateListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            AnimationSet animationSet = new AnimationSet(true);
            /**
             * 参数1~2,X轴的开始位置
             * 参数3~4,Y轴的开始位置
             * 参数5~6,X轴的结束位置
             * 参数7~8,X轴的结束位置
             * */
            TranslateAnimation translateAnimation = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,0.f,
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,0.5f
            );

            translateAnimation.setDuration(1000);
            animationSet.addAnimation(translateAnimation);
            translateAnimation.setFillAfter(true);
            mIv.startAnimation(animationSet);
        }
    }

    private void addFocusAnimator() {
        ObjectAnimator objectAnimator_1 = ObjectAnimator.ofFloat(this, "scaleX", 1f, 1.01f);
        ObjectAnimator objectAnimator_2 = ObjectAnimator.ofFloat(this, "scaleY", 1f, 1.01f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(300);
        animatorSet.playTogether(objectAnimator_1, objectAnimator_2);
        animatorSet.start();
    }

    private void cutFocusAnimator() {
        ObjectAnimator objectAnimator_1 = ObjectAnimator.ofFloat(this, "scaleX", 1.01f, 1f);
        ObjectAnimator objectAnimator_2 = ObjectAnimator.ofFloat(this, "scaleY", 1.01f, 1f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(300);
        animatorSet.playTogether(objectAnimator_1, objectAnimator_2);
        animatorSet.start();
    }

}
